package com.hepinghospital.user.adapter

import android.annotation.SuppressLint
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hepinghospital.user.R
import com.rest.response.MaterialBean
import jz.joyoung.robot.util.UtilString

/**
 * @Description: 耗材列表适配器
 * @Author: lzp
 * @CreateDate: 2022/3/9 15:01
 * @UpdateRemark: 更新说明
 */
class ConsumeMaterialAdapter(data: MutableList<MaterialBean>) : BaseQuickAdapter<MaterialBean, BaseViewHolder>(R.layout.item_consume_material, data) {
//    private var waitedTotalAmount = 0.0
//    private var totalAmount = 0.0
//    private var isWait = false

    @SuppressLint("SetTextI18n")
    override fun convert(holder: BaseViewHolder, item: MaterialBean) {
        holder.setText(R.id.tv_consumeMaterials_orderId, "订单编号：${item.orderNumber}")
        holder.setText(R.id.tv_consumeMaterials_status, if (item.orderStatus == 10) "待支付" else "已支付")

//        holder.setText(R.id.tv_consumeMaterials, item.consumables)
////        holder.setText(R.id.tv_number, "${item.number}")
////        totalAmount += item.orderAmount.toDouble()

        val totalSpannable = SpannableStringBuilder("")
        for ((index, bean) in item.items.withIndex()) {
            val spannable = SpannableStringBuilder("${bean.consumables}*${bean.number}")
            if (index != item.items.size - 1) {
                spannable.append("\n")
            }
            spannable.setSpan(ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_F54C4C)), bean.consumables.length + 1, spannable.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            totalSpannable.append(spannable)
        }
        holder.setText(R.id.tv_consumeMaterials, totalSpannable)

        val spannable = SpannableStringBuilder("耗材价格：${UtilString.stringToDouble(item.orderAmount).div(100)}元")
        spannable.setSpan(ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_F54C4C)), 5, spannable.length - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        holder.setText(R.id.tv_consumeMaterials_price, spannable)

        when (item.orderStatus) {
            10 -> {
                holder.setGone(R.id.tv_consumeMaterials_pay, false)
                holder.setGone(R.id.tv_consumeMaterials_status, true)
//            waitedTotalAmount += item.orderAmount.toDouble()
//            isWait = true
            }
            35 -> {
                holder.setGone(R.id.tv_consumeMaterials_pay, true)
                holder.setGone(R.id.tv_consumeMaterials_status, false)
                holder.setText(R.id.tv_consumeMaterials_status, "已支付")
            }
            else -> {
                holder.setGone(R.id.tv_consumeMaterials_pay, true)
                holder.setGone(R.id.tv_consumeMaterials_status, false)
                holder.setText(R.id.tv_consumeMaterials_status, "支付中")
            }
        }
    }

    //是否还有待支付的耗材订单
    fun show(): Boolean {
        var isWait = false
        for (bean in data) {
            if (bean.orderStatus == 10) {
                isWait = true
            }
        }
        return isWait
    }

    //待支付耗材的总额
    fun getWaitedTotalAmount(): Double {
        var waitedTotalAmount = 0.0
        for (bean in data) {
            if (bean.orderStatus == 10) {
                waitedTotalAmount += UtilString.stringToDouble(bean.orderAmount)
            }
        }
        return waitedTotalAmount
    }

    //一个护理订单下 所有耗材订单的总价
    fun getConsumeMaterialAmount(): Double {
        var totalAmount = 0.0
        for (bean in data) {
//            totalAmount += bean.orderAmount.toDouble()
            totalAmount += UtilString.stringToDouble(bean.orderAmount)

        }
        return totalAmount
    }
}